<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
    "http://www.w3.org/TR/html4/strict.dtd"
    >
<html lang="en">
<head>
<title>[%description.namespace%]::[%table.namespace%]::Редактирование::</title>
<script src="/j/lib/jquery-1.6.2.min.js"></script>
<script src="/j/lib/jquery.tmpl.min.js"></script>
<script src="/j/mc/mc.types.js"></script>
<script>
var Descriptions = [%description.json(1)%];
var path_to_img = "[%path_to_img%]";
</script>

<script id="times_tmpl" type="text/x-jquery-tmpl">
<b>Сейчас:</b> ${ ts_time(now) }
{{if last}}
| <b>Актуально:</b> ${ ts_full(last) } | Прошло: ${ ts_diff(now - last) } |
{{/if}}
<a href="?ref=[%ref%];cmd=renew;r=[%rand%]">Обновить</a>
</script>

<script id="page_tmpl" type="text/x-jquery-tmpl">
<a href="#" onclick="return get_page(${page})">${page+1}</a>
</script>


<script id="pager_tmpl" type="text/x-jquery-tmpl">
Страницы: 
	{{if f_page != c_page}}
	{{tmpl({page:f_page}) "#page_tmpl"}}
	...
	{{/if}}
	{{if f_page < c_page - 1}}
	{{tmpl({page:c_page - 1}) "#page_tmpl"}}
	{{/if}}

	<b>${c_page - 0 + 1}</b>

	{{if l_page > c_page - 0 + 1}}
	{{tmpl({page:c_page - 0 + 1}) "#page_tmpl"}}
	{{/if}}
	{{if l_page != c_page}}
	...
	{{tmpl({page:l_page}) "#page_tmpl"}}
	{{/if}}
</script>


<script id="form_tmpl" type="text/x-jquery-tmpl">
<form onsubmit="return submit_form2(this)">
<input type=hidden name="cmd" value="save">
<table>
{{if [%table.pkey%]}}
<tr>
	<th>[%description.pkey%]</th>
	<td>
		${[%table.pkey%]}
		<input type=hidden name="row.[%table.pkey%]" value="${[%table.pkey%]}">
	</td>
</tr>
{{/if}}
[%for f in table.fields%]
<tr>
	<td>
		[%description.fields.$f.title%]
		<br><i>[%f%]</i>
	</td>
	<td>
		{{html mc.types.draw_input(Descriptions.fields.[%f%],"row.[%f%]",$data.[%f%]) }}
	</td>
</tr>
[%end%]
<tr>
	<td><input type=submit value="Сохранить"></td>
	<td><button onclick="return close_form()">Закрыть</button></td>
</tr>
</table>
</form>
</script>

<script id="row_tmpl" type="text/x-jquery-tmpl">
<td>
	<input type="checkbox" name="row_id" value="${[%table.pkey%]}">
</td>
<td>${[%table.pkey%]}</td>
[%for f in table.fields%]
<td>
	[%if description.fields.$f.hide%]
		<div style="float:left;width:10px;cursor:pointer" onclick="onoff(this)">+</div>
		<div style="display:none;overflow:hidden">
			{{html mc.types.draw_value(Descriptions.fields.[%f%],$data.[%f%]) }}
		</div>
	[%else%]
		[%if description.fields.$f.html%]
			{{html mc.types.draw_value(Descriptions.fields.[%f%],$data.[%f%]) }}
		[%elsif description.fields.$f.code%]
			<pre>${ mc.types.draw_value(Descriptions.fields.[%f%],$data.[%f%]) }</pre>
		[%else%]
			${ mc.types.draw_value(Descriptions.fields.[%f%],$data.[%f%]) }
		[%end%]
	[%end%]
</td>
[%end%]
<td>
	<button onclick="return edit_row(this)">edit</button>
</td>
</script>

<script id="table_tmpl" type="text/x-jquery-tmpl">
<form onsubmit="return submit_form(this)">
<table>
<thead>
<tr>
	<td colspan=[%table.fields.size + 3%]>
	{{tmpl "#pager_tmpl"}}
	</td>
</tr>
<tr>
	<th colspan=2>[%description.pkey%]</th>
	[%for f in table.fields%]
	<th>[%description.fields.$f.title%]</th>
	[%end%]
	<th><button onclick="return edit_row()">new</button></th>
</tr>
</thead>
<tbody>
{{each list}}
	<tr>{{tmpl($value) "#row_tmpl"}}</tr>
{{/each}}
</tbody>
<tfoot>
<tr>
	<td colspan=2><input type=submit value="Удалить"></td>
	<td colspan=[%table.fields.size + 1%]>
	{{tmpl "#pager_tmpl"}}
	</td>
</tr>
</tfoot>
</table>
</form>
</script>
<script>
var current_page = 0;
$(function(){
	$("#times_tmpl").tmpl({last: [%last_new%], now: [%now_time%]}).appendTo("#times");
	get_page();
});
function send(data){
	$.ajax({
		url: "?",
		type: "POST",
		data: data,
		dataType: "json",
		traditional: true,
		success: draw
	});
};
function get_page(page){
	send({
		cmd: "page",
		ref: "[%ref%]",
		page: page || 0
	});
	return false;
}
function draw(data){
	current_page = data.c_page;
	$("#table_tmpl").tmpl(data).appendTo($("#table").empty());
}
function edit_row(button){
	var data = {};
	if(button){
		data = $(button).tmplItem().data;
	}
	$("#form_tmpl").tmpl(data).appendTo($("#form").empty());
	return false;
}
function submit_form(form){
	var data = [];
	if(form.row_id.length){
		for(var i = 0; i < form.row_id.length; i++){
			if(form.row_id[i].checked){
				data.push(form.row_id[i].value);
			}
		}
	}else if(form.row_id.checked){
		data.push(form.row_id.value);
	}
	if(data.length > 0 ){
		send({
			cmd: "delete",
			row_id: data,
			ref: "[%ref%]",
			page: current_page
		});
	}
	return false;
}
function close_form(form){
	$("#form").empty();
	return false;
}
function submit_form2(form){
	var data = $(form).serializeArray();
	data.push({name:"page",value: current_page});
	data.push({name:"ref",value: "[%ref%]"});
	send(data);
	$("#form").empty();
	return false;
}
function normalize(data){
	var ret = [];
	var row = {};
	for(var i = 0 ; i < data.length; i++){
		var path = data[i].title.split(".");
		if(path[0] != "row"){
			ret.push(data[i]);
			break;
		}
	}
}
function prepare(root){
	var row = {};
	for(var f in root){
		var s = root[f];
		if(s.type == "array"){
			row[f] = prepare
		}else if(s.type == "hash"){
		}
	}
}
function onoff(div){
	var o = $(div);
	var t = o.parent().find(">div:eq(1)");
	if(o.html() == "+"){
		t.show();
		o.html("-");
	}else{
		t.hide();
		o.html("+");
	}
	return false;
}
function ts_time(now){
	return (new Date(now*1000)).toLocaleTimeString();
}
function ts_full(last){
	return (new Date(last*1000)).toLocaleString();
}
function ts_diff(diff){
	var s = diff % 60;
	var m = Math.floor(diff / 60) % 60;
	var h = Math.floor(diff / 60 / 60) % 24;
	var d = Math.floor(diff / 60 / 60 / 24);
	return "" + (d > 0 ? (d + "д. ") : "") + (h<10?"0":"") + h + ":" + (m<10?"0":"") + m + ":" + (s<10?"0":"") + s;
}
</script>
<style>
#table table,
#form table {
	margin-top : 5px;
}
#table table,
#table table th,
#table table td,
#form table,
#form table th,
#form table td
{
	border-collapse: collapse;
	border: 1px solid black;
}
#form table td,
#table table td {
	vertical-align: top;
}
#form table td,
#table table td,
#form table th,
#table table th {
	padding: 2px;
}
#menu a:link,
#menu a:active,
#menu a:visited {
	color: blue;
}
#menu a.bold {
	font-weight: bold;
}
#upload{
	overflow: hidden;
}
#menu {
	border-bottom: 3px double black;
	margin-bottom: 3px;
	padding-bottom: 6px;
}
</style>
</head>
<body>
	<div id="menu">
	[%for o in list%]
		[ <a href="?ref=[%o.0%];r=[%rand%]"[%if o.0 == ref;' class="bold"';end%]>[%o.1%]</a> ]
	[%end%]
	</div>
	<div id="times"></div>
	<div id="upload">
		<div style="float:left">
			<a href="?cmd=export;ref=[%ref%];r=[%rand%]" target=_blank>Экпорт</a>
		</div>
		[%unless upl%]
			<div style="float:left">
				<form method=post enctype="multipart/form-data" action="?">
				<input type="hidden" name="cmd" value="import"/>
				<input type="hidden" name="ref" value="[%ref%]"/>
				<input type="file" name="file_for_import"/>
				<input type="submit" value="Импорт"/>
				</form>
			</div>
			<div>
			[%if file%]
				Загружен: <b>"[%file%]"</b><br>
				type="[%filetype%]"<br>
				Результат: [%result%]
			[%end%]
			</div>
		[%end%]
	</div>
	<div id="table"></div>
	<div id="page"></div>
	<div id="form"></div>
	[%if filedata%]
		<pre>[%filedata.json(1)%]</pre>
	[%end%]
</body>
</html>
